<?php

/**
 * Created by PhpStorm.
 * User: Win 8 64Bit VS7
 * Date: 1/1/14
 * Time: 12:19 PM
 */
class WebUser extends CWebUser {
    private $_model;

    // access it by Yii::app()->user->username
    public function getFullName() {
        $user = $this->loadUser(Yii::app()->user->id);
        return $user != null
            ? ($user->user_name == "" ? $user->email : $user->user_name)
            : "";
    }

    public function getEmail() {
        $user = $this->loadUser(Yii::app()->user->id);
        return $user == null ? "" : $user->email;
    }

    public function getAllow() {
        $user = $this->loadUser(Yii::app()->user->id);
        return $user == null ? "" : $user->user_allow;
    }

    // This is a function that checks the field 'role'
    // in the User model to be equal to 1, that means it's allowAdmin
    // access it by Yii::app()->user->allowAdmin()
    public function isAdmin() {
        $user = $this->loadUser(Yii::app()->user->id);
        if ($user == null) return "false";
        return $user->role == UserRoleEnum::admin ? "true" : "false";
    }

    // Load user model.
    protected function loadUser($id = null) {
        if ($this->_model === null) {
            if ($id !== null)
                $this->_model = User::model()->findByPk($id);
        }
        return $this->_model;
    }
} 